// cd /projects/hsieh_project/proj_201809/code_2_201910/
// qstata out_cind_con_city_emp.do &

set linesize 255
capture log close
log using /projects/hsieh_project/proj_201809/code_2_201910/out_cind_con_city_emp_log, replace text

clear all
capture noi program drop _all
cd /projects/

di "Started at $S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/201910_main/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/201910_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/cind_con_city_emp/"
capture mkdir ${dir_outf}

global year1 = 1977
global year2 = 2013

global gl_perc = "10"
local l_perc = "10"

// Load functions
do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_lpolyci.do"

//==============================================================================
// Levels in City

/*
--------------------------------------------------------------------------------
Function that reads in the city-ind data
i_city = city code
v_weight = weight variable used to aggregate from city-ind to city level
v_sector = variable name of sector variable
c_mfg = 1 = using MfG only
*/

capture program drop read_city_lvl
program read_city_lvl
	args i_city v_weight v_sector c_mfg
	
	// Can use cind_emp or w_sv as weight
	
	di "`i_city' `v_weight' `v_sector'"
	//local i_city = "msa"
	use "${dir_data}/cityind_sum_all_sel", clear
	keep if city_code == "`i_city'"
	keep if year1 == ${year1} & year2 == ${year2}
	
	// Employment share
	gen cind_emps_ind_10 = emp_ind_10 / emp_cind
	gen cind_emps_ind_1 = emp_ind_1 / emp_cind
	gen cind_emps_cind_t1 = emp_cind_t1 / emp_cind
	
	gen c_nconv = 1/n_cind
	
	if "`c_mfg'" == "1" {
		keep if sector == 1
	}
	
	// Calculate weighted average at the city level
	collapse (first) emp_city_${year1} emp_city_${year2} ///
		(mean) c_emps_ind_10=cind_emps_ind_10 c_emps_ind_1=cind_emps_ind_1 ///
		c_emps_cind_t1=cind_emps_cind_t1 c_hhi=hhi_cind c_hhin=hhin_cind c_n=n_cind c_nconv [w=`v_weight'], by(city year `v_sector')
	
	sort city year
	by city: gen n_year = _N
	tab n_year
	
	// ln(levels)
	foreach i_var of var emp_city_${year1} c_emps_ind_10 c_emps_ind_1 c_emps_cind_t1 {
		gen ln_`i_var' = ln(`i_var')
	}

end

/*
--------------------------------------------------------------------------------
Function that plots
Y = city-ind concentration aggregated to city level
X = initial city size
*/

capture program drop fig_city_lvl
program fig_city_lvl
	args i_city i_var t_ytitle t_title t_subtitle c_weight c_mfg
	if 0 {
	twoway (scatter `i_var' ln_emp_city_${year1} if year == ${year1}, msize(small)) ///
		(scatter `i_var' ln_emp_city_${year1} if year == ${year2}, msize(small)), ///
		ytitle("`t_ytitle'") xtitle("ln(City Employment in ${year1})") ///
		legend(order(1 "1977" 2 "2013")) ///
		title("`t_title'") subtitle("`t_subtitle'")
	}
	//graph export "${dir_outf}/scat_`i_var'_c_emp_${year1}_`i_city'`c_weight'.png", replace width(3000) height(2000)
	
	capture drop `i_var'_1
	capture drop `i_var'_2
	gen `i_var'_1 = `i_var' if year == ${year1}
	gen `i_var'_2 = `i_var' if year == ${year2}
	
	lpolyci_data "" "lpoly_fig1" "`i_var'_1" "ln_emp_city_${year1}" "99" "100"
	lpolyci_data "" "lpoly_fig2" "`i_var'_2" "ln_emp_city_${year1}" "99" "100"
	
	frame lpoly_fig1 {
		preserve
		keep `i_var'_1_x `i_var'_1_s `i_var'_1_se
		rounding_4dig "`i_var'_1_x `i_var'_1_s `i_var'_1_se"
		export excel ${dir_outf}/lpoly_cind_con_city_emp.xlsx, sheet("`i_city'_`i_var'_${year1}") sheetreplace firstrow(var) keepcellfmt
		restore
	}
	frame lpoly_fig2 {
		preserve
		keep `i_var'_2_x `i_var'_2_s `i_var'_2_se
		rounding_4dig "`i_var'_2_x `i_var'_2_s `i_var'_2_se"
		export excel ${dir_outf}/lpoly_cind_con_city_emp.xlsx, sheet("`i_city'_`i_var'_${year2}") sheetreplace firstrow(var) keepcellfmt
		restore
	}
	
	frame lpoly_fig1 {
		frlink 1:1 row_id, frame(lpoly_fig2)
		frget _all, from(lpoly_fig2)
		
		local v_y1 = "`i_var'_1"
		local v_y2 = "`i_var'_2"
		
		if "`i_city'"=="fips" {
			foreach i_suf in cil ciu s x {
				replace `v_y1'_`i_suf' = . if `v_y1'_x <5
				replace `v_y2'_`i_suf' = . if `v_y2'_x <5
			}
			local c_xlab "xlab(5(2)15)"
		}
		else if inlist("`i_city'","msa","msa0") {
			foreach i_suf in cil ciu s x {
				replace `v_y1'_`i_suf' = . if `v_y1'_x <9
				replace `v_y2'_`i_suf' = . if `v_y2'_x <9
			}
			local c_xlab "xlab(9(2)15)"
		}
		
		if 0 {
		twoway (rarea `v_y1'_cil `v_y1'_ciu `v_y1'_x, color(none) fi(inten0) lc(gs12) lp(dash) ) ///
			(connected `v_y1'_s `v_y1'_x, m(i) lcolor(navy) lw(medthick) lp(dash_dot)) ///
			(rarea `v_y2'_cil `v_y2'_ciu `v_y2'_x, color(none) fi(inten0) lc(gs12) lp(dash)) ///
			(connected `v_y2'_s `v_y2'_x, m(i) lcolor(maroon) lw(medthick)), ///
			legend(order(2 "${year1}")) ///
			ytitle("`t_ytitle'") xtitle("ln(City Employment in ${year1})") ///
			title("`t_title'") subtitle("`t_subtitle'") `c_xlab'
		graph export "${dir_outf}/lpoly_`i_var'_c_emp_${year1}_`i_city'`c_weight'.png", replace width(3000) height(2000)
		}
		
		local t_sample = "Sample: ALL, IND10, IND10_I"
		if "`c_mfg'" == "1" local t_sample = "Sample: ALL, ALL_S1, IND10_S1, ALL_S*, IND10*"
		
		twoway (rarea `v_y1'_cil `v_y1'_ciu `v_y1'_x, color(none) fi(inten0) lc(gs8) lw(thin)) ///
			(connected `v_y1'_s `v_y1'_x, m(i) lcolor(navy) lw(medthick)), ///
			legend(order(2 "${year1}")) ///
			ytitle("`t_ytitle'") xtitle("ln(City Employment in ${year1})") ///
			title("`t_title'") subtitle("`t_subtitle', 99% CI") `c_xlab' note("`t_sample'")
		graph export "${dir_outf}/lpoly_`i_var'_c_emp_${year1}_y1_`i_city'`c_weight'.png", replace width(3000) height(2500)
		
		twoway (rarea `v_y2'_cil `v_y2'_ciu `v_y2'_x, color(none) fi(inten0) lc(gs8) lw(thin)) ///
			(connected `v_y2'_s `v_y2'_x, m(i) lcolor(maroon) lw(medthick)), ///
			legend(order(2 "${year2}")) ///
			ytitle("`t_ytitle'") xtitle("ln(City Employment in ${year1})") ///
			title("`t_title'") subtitle("`t_subtitle', 99% CI") `c_xlab' note("`t_sample'")
		graph export "${dir_outf}/lpoly_`i_var'_c_emp_${year1}_y2_`i_city'`c_weight'.png", replace width(3000) height(2500)
		
		twoway (connected `v_y1'_s `v_y1'_x, m(i) lcolor(navy) lw(medthick)) ///
			(connected `v_y2'_s `v_y2'_x, m(i) lcolor(maroon) lw(medthick)), ///
			legend(order(1 "${year1}" 2 "${year2}")) ///
			ytitle("`t_ytitle'") xtitle("ln(City Employment in ${year1})") ///
			title("`t_title'") subtitle("`t_subtitle', 99% CI") `c_xlab' note("`t_sample'")
		graph export "${dir_outf}/lpoly_`i_var'_c_emp_${year1}_y1y2_`i_city'`c_weight'.png", replace width(3000) height(2500)
		
	}
end

capture program drop fig_group
program fig_group
	args c_weight t_weight f_weight c_mfg
	
	fig_city_lvl "${i_city}" "ln_c_emps_ind_10" "ln(Share of Top 10% Ind Firm)" "ln(Share of Top 10% Ind Firm)" "${t_city}`t_weight'" "`f_weight'" "`c_mfg'"
	//fig_city_lvl "${i_city}" "ln_c_emps_ind_1" "ln(Share of Top 1% Ind Firm)" "ln(Share of Top 1% Ind Firm)" "${t_city}`t_weight'" "`f_weight'" "`c_mfg'"
	//fig_city_lvl "${i_city}" "ln_c_emps_cind_t1" "ln(Share of Top 1 City-Ind Firm)" "ln(Share of Top 1 City-Ind Firm)" "${t_city}`t_weight'" "`f_weight'" "`c_mfg'"
	//fig_city_lvl "${i_city}" "c_hhi" "City Average of City-Ind HHI" "City Average of City-Ind HHI" "${t_city}`t_weight'" "`f_weight'" "`c_mfg'"
end

//==============================================================================
// Aggreate across all industries

global dir_outf "${dir_out}/cind_con_city_emp"
capture mkdir ${dir_outf}

*Only outputting for msa1983: 

/*
// MSA
global i_city = "msa"
global t_city = upper("${i_city}")
// city-ind weight
read_city_lvl "${i_city}" "emp_cind" "" "0"
fig_group "emp_cind" "" ""
if 1 {
// MSA0
drop if city < 60
global i_city = "msa0"
global t_city = upper("${i_city}")
fig_group "emp_cind" "" ""
}

// SV weight
//read_city_lvl "${i_city}" "w_sv"
//fig_group "emp_cind" ", SV Weighted" "_sv"

// FIPS
global i_city = "fips"
global t_city = upper("${i_city}")
// city-ind weight
read_city_lvl "${i_city}" "emp_cind"
fig_group "emp_cind" "" ""
// SV weight
//read_city_lvl "${i_city}" "w_sv"
//fig_group "emp_cind" ", SV Weighted" "_sv"

// CZONE
global i_city = "czone"
global t_city = upper("${i_city}")
// city-ind weight
read_city_lvl "${i_city}" "emp_cind"
fig_group "emp_cind" "" ""
*/

// MSA1983
global i_city = "msa1983"
global t_city = upper("${i_city}")
// city-ind weight
read_city_lvl "${i_city}" "emp_cind" "" "0"
fig_group "emp_cind" "" ""

/* FOR SUMMARY TABLE */
preserve
collapse (mean) c_emps_ind_10 c_hhi c_emps_cind_t1 (count) count_msa1983 = city, by(year)
save ${dir_proj}/output/202011_main/summary_stats/E_msa1983_top_emp_share, replace
restore
/*

//------------------------------------------------------------------------------
// Improved MSA*CZ

// MSA1983CZ
global i_city = "msa1983cz"
global t_city = upper("${i_city}")
// city-ind weight
read_city_lvl "${i_city}" "emp_cind" "" ""
global i_city = "msa1983cz"
fig_group "emp_cind" "" ""

preserve
global i_city = "msa83czm"
global t_city = upper("${i_city}")
keep if city < 10000
fig_group "emp_cind" "" ""
restore
preserve
global i_city = "msa83czc"
global t_city = upper("${i_city}")
keep if city > 10000
fig_group "emp_cind" "" ""
restore

*/

//==============================================================================
// Aggreate across MfG

global dir_outf "${dir_out}/cind_con_city_emp_mfg"
capture mkdir ${dir_outf}

*ONLY OUTPUTTING FOR MSA1983:

// MSA1983
global i_city = "msa1983"
global t_city = upper("${i_city}")
// city-ind weight
read_city_lvl "${i_city}" "emp_cind" "" "1"
fig_group "emp_cind" ", MfG" "_mfg" "1"
// SV weight
//read_city_lvl "${i_city}" "w_sv" "" "1"
//fig_group "emp_cind" ", MfG, SV Weighted" "_sv"


/*
// MSA
global i_city = "msa"
global t_city = upper("${i_city}")
// city-ind weight
read_city_lvl "${i_city}" "emp_cind" "" "1"
fig_group "emp_cind" ", MfG" "_mfg" "1"
if 0 {
// MSA0
drop if city < 60
global i_city = "msa0"
global t_city = upper("${i_city}")
fig_group "emp_cind" ", MfG" "_mfg" "1"
}

// SV weight
//read_city_lvl "${i_city}" "w_sv" "" "1"
//fig_group "emp_cind" ", MfG, SV Weighted" "_sv"

// FIPS
global i_city = "fips"
global t_city = upper("${i_city}")
// city-ind weight
read_city_lvl "${i_city}" "emp_cind" "" "1"
fig_group "emp_cind" ", MfG" "_mfg" "1"
// SV weight
//read_city_lvl "${i_city}" "w_sv" "" "1"
//fig_group "emp_cind" ", MfG, SV Weighted" "_sv"

// CZONE
global i_city = "czone"
global t_city = upper("${i_city}")
// city-ind weight
read_city_lvl "${i_city}" "emp_cind" "" "1"
fig_group "emp_cind" ", MfG" "_mfg" "1"

*/
di "Ended at $S_DATE $S_TIME"
capture noi log close
// End of do file
